package com.xy.jm.user.controller;
import cn.hutool.core.util.NumberUtil;
import com.xy.common.base.controller.BaseController;
import com.xy.common.base.entity.AjaxResult;
import com.xy.jm.service.JmAuthenticateService;
import com.xy.jm.service.JmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/jm/user/")
public class UserController extends BaseController {
    @Autowired
    private JmUserService jmUserService;
    @Autowired
    private JmAuthenticateService jmAuthenticateService;
    @RequestMapping("root/list")
    public AjaxResult GetRootListData(HttpServletRequest request){
        Map<String,Object> data=new HashMap<>();
        data.put("list",jmUserService.get_root_list().get(0));
        String ip = request.getLocalAddr();
        // 取得服务器端口
        int port = request.getLocalPort();
        data.put("ip",ip+":"+port);
        return success(data);
    }
    @RequestMapping(value = "find/authenticate",method = RequestMethod.POST)
    public AjaxResult FindAuthenticateData(HttpServletRequest request){
        try {
            Map<String,Object> data=new HashMap<>();
            String parameter_authenticate_id = request.getParameter("authenticate_id");
            if(!NumberUtil.isInteger(parameter_authenticate_id)){
                    throw new Exception("authenticate_id:数据类型错误");
            }
            data.put("authenticate_info",jmAuthenticateService.find_by_id(Integer.parseInt(parameter_authenticate_id)));
            String ip = request.getLocalAddr();
            // 取得服务器端口
            int port = request.getLocalPort();
            data.put("ip",ip+":"+port);
            return success(data);
        }catch (Exception e){
            return dealException(e);
        }
    }
}
